/**
 * Author: Dmitry Ustimov
 * Date: 5/31/13
 * Time: 8:14 PM
 */

package com.gmail.dmitryustimov.ms.java7.mediator.mod.work.demo.col10event1;

import com.gmail.dmitryustimov.ms.java7.patternslib.mediator.Colleague;
import com.gmail.dmitryustimov.ms.java7.patternslib.mediator.EventPublisher;
import com.gmail.dmitryustimov.ms.java7.patternslib.mediator.SubscriberAction;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Mediator implements Colleague {

    private String name;
    private List<Colleague> colleagueList = new ArrayList<>();
    private BigDecimal sum = BigDecimal.valueOf(0);

    private EventPublisher<Event1> event1Publisher = new EventPublisher<>();

    public Mediator(String name) {
        this.name = name;
    }

    public void addColleague(AbstractColleague colleague) {
        colleagueList.add(colleague);
        colleague.getEventPublisher().subscribe(this.new Event1Action());
    }

    private class Event1Action extends SubscriberAction<Event1> {
        public Event1Action() {
            super(Mediator.this);
        }

        @Override
        public void process(Event1 event, Colleague originator) {
            sum.add(BigDecimal.valueOf(1));
            event1Publisher.publish(event, originator);
        }
    }

    public EventPublisher<Event1> getEvent1Publisher () {
        return event1Publisher;
    }

    public void setEvent1Publisher(EventPublisher<Event1> event1Publisher) {
        this.event1Publisher = event1Publisher;
    }
}
